package com.xiaobawang.xjsyy_idea_plugin.panel.config.view;

import com.intellij.openapi.application.ApplicationManager;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;

/**
 * @author synhard
 * @description TODO
 * @date 2024/5/23 10:54
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class GitRepoPanel extends JPanel {
    private JTextField gitRepoTextField;
    private JButton setButton;

    // 定义 GitRepo 更新的回调接口
    public interface OnGitRepoUpdateListener {
        void onGitRepoUpdated();
    }

    private OnGitRepoUpdateListener gitRepoUpdateListener;

    // 在 doOKAction 中调用这个方法来更新 gitRepoTextField
    private void updateGitRepoDisplay() {
        gitRepoTextField.setText(GlobalConfig.gitRepo);
    }

    public GitRepoPanel(OnGitRepoUpdateListener gitRepoUpdateListener) {
        this.gitRepoUpdateListener = gitRepoUpdateListener;
        if (!StringUtils.isEmpty(GlobalConfig.gitRepo)) {
            this.gitRepoTextField = new JTextField(GlobalConfig.gitRepo);
        } else {
            this.gitRepoTextField = new JTextField("请配置代码仓库链接");
        }
        this.gitRepoTextField.setEditable(false);
        this.setButton = new JButton("设置");

        setButton.addActionListener(e -> {
            ApplicationManager.getApplication().invokeLater(() -> {
                if (new GitRepoDialog(this::updateGitRepoDisplay).showAndGet()) {
                    if (this.gitRepoUpdateListener != null) {
                        this.gitRepoUpdateListener.onGitRepoUpdated();
                    }
                }
            });
            super.repaint();
        });

        add(new JLabel("git repo: "));
        add(this.gitRepoTextField);
        add(this.setButton);
    }
}
